package com.bdsy.business.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bdsy.business.dao.AuctionShopMapper;
import com.bdsy.business.dao.AuctionTransRecordMapper;
import com.bdsy.business.model.AuctionShop;
import com.bdsy.business.model.AuctionTransRecord;
import com.bdsy.business.service.AuctionTransRecordService;
import com.bdsy.common.utils.DateUtil;

@Service
public class AuctionTransRecordServiceImpl implements AuctionTransRecordService {

	@Autowired
	private AuctionTransRecordMapper auctionTransRecordMapper;
	
	@Autowired
	private AuctionShopMapper auctionShopMapper;
	
	
	@Override
	public boolean add(AuctionTransRecord auctionTransRecord) {
		//添加之前 根据用户 shopId 判断是否为空
		List<AuctionTransRecord> aTransRecord = auctionTransRecordMapper.selectAuctionTransRecordById(auctionTransRecord.getShopId(), auctionTransRecord.getUserId());
		int result = 0;
		
		if(aTransRecord == null || aTransRecord.size() == 0) {
			auctionTransRecord.setIncreCount(0);
			auctionTransRecord.setIncreTime(DateUtil.getCurrentDate());
			result = auctionTransRecordMapper.add(auctionTransRecord);
		}else {
			auctionTransRecord.setId(aTransRecord.get(0).getId());
			auctionTransRecord.setIncreTime(DateUtil.getCurrentDate());
			auctionTransRecord.setAuctionPrice(String.valueOf(Integer.parseInt(aTransRecord.get(0).getAuctionPrice())+ Integer.parseInt(auctionTransRecord.getAuctionPrice())));
			auctionTransRecord.setIncreCount(aTransRecord.get(0).getIncreCount()+1);
			result = auctionTransRecordMapper.update(auctionTransRecord);
		}
		return result>0?true:false;
	}

	@Override
	public boolean addAuctionTransRecord(AuctionTransRecord auctionTransRecord) {
		
		int result = 0;
		auctionTransRecord.setIncreTime(DateUtil.getCurrentDate());
		result =  auctionTransRecordMapper.add(auctionTransRecord);
		// 获取拍卖商品此时的价格
		List<AuctionShop> auctShop = auctionShopMapper.selectAuctionShopByShopId(auctionTransRecord.getShopId());
		
		//修改商品拍卖价格
		AuctionShop auctionShop = new AuctionShop();
		auctionShop.setShopId(auctionTransRecord.getShopId());
		auctionShop.setIncrePrice(String.valueOf(Integer.parseInt(auctShop.get(0).getIncrePrice()) + Integer.parseInt(auctionTransRecord.getAuctionPrice())));
		auctionShopMapper.update(auctionShop);
		
		return result>0?true:false;
	}

	@Override
	public int count(int shopId) {
		int totalCount = auctionTransRecordMapper.count(shopId);
		return totalCount;
	}

	@Override
	public List<Map> selectAuctionTransRecordByShopId(int shopId) {
		List<Map> auctionTransRecord = auctionTransRecordMapper.selectAuctionTransRecordByShopId(shopId);
		return auctionTransRecord;
	}

	@Override
	public List<Map> selectAuctionTransRecordByUserId(String userId) {
		List<Map> auctionTransRecordList = auctionTransRecordMapper.selectAuctionTransRecordByUserId(userId);
		
		for (int i = 0; i < auctionTransRecordList.size(); i++) {
			for (Map map : auctionTransRecordList) {
				double increPrice = Double.parseDouble((String) map.get("incre_price"));
				double ShopPrice = Double.parseDouble((String) map.get("shop_price"));
				double totalAuctPrice = (double) map.get("total_auct_price");
				
				System.out.println(increPrice + "----" +  ShopPrice + "----" +totalAuctPrice);
				
				if((ShopPrice + totalAuctPrice) >= increPrice) {
					map.put("promsg","出价最高");
					System.out.println("出价最高");
				}else {
					System.out.println("已被超越");
					map.put("promsg","已被超越");
				}
			}
		}
		
		return auctionTransRecordList;
	}

	@Override
	public List<Map> auctionTransRecordByUserId(String userId) {
		return auctionTransRecordMapper.auctionTransRecordByUserId(userId);
	}

}
